﻿using System;
using System.Collections.Generic;
using System.Linq;
using TaskKernel.DTO;
using Xunit;

namespace TaskKernel.Tests
{
    public class ExtensionsTest
    {
        [Fact]
        public void QueueAbstractSortFieldToString()
        {
            var queue = new Queue<AbstractSortField>();
            queue.Enqueue(new AbstractSortField("Create", true));
            queue.Enqueue(new AbstractSortField("Close", false));
            queue.Enqueue(new AbstractSortField("Update", true));
            var s = queue.ToStringLine();
            Assert.Equal("Create_ASC:Close_DESC:Update_ASC", s);
        }

        [Fact]
        public void QueueAbstractSortFieldFromString()
        {
            string str = "Create_ASC:Close_DESC:Update_ASC";
            var queue = str.ToQueue();
            Assert.Equal(3, queue.Count);
            Assert.True(queue.Dequeue().SortDirectionAsc);
            Assert.Equal("Close", queue.Dequeue().Field);
        }

        [Fact]
        public void SortByOneFieldASC()
        {
            List<ExecutorsModel> executors = GetExecutors();
            var sortqueue = new Queue<AbstractSortField>();
            sortqueue.Enqueue(new AbstractSortField("TaskName", true));
            executors = executors.Select(x => x.SortByMany(new Queue<AbstractSortField>(sortqueue))).ToList();
            Assert.Equal("Задача1", executors.ElementAt(0).TaskList.ElementAt(0).TaskName);
            Assert.Equal("Задача1", executors.ElementAt(1).TaskList.ElementAt(0).TaskName);
        }

        [Fact]
        public void SortByOneFieldDESC()
        {
            List<ExecutorsModel> executors = GetExecutors();
            var sortqueue = new Queue<AbstractSortField>();
            sortqueue.Enqueue(new AbstractSortField("TaskName", false));
            executors = executors.Select(x => x.SortByMany(new Queue<AbstractSortField>(sortqueue))).ToList();
            Assert.Equal("Задача3", executors.ElementAt(0).TaskList.ElementAt(0).TaskName);
            Assert.Equal("Задача3", executors.ElementAt(1).TaskList.ElementAt(0).TaskName);
        }

        [Fact]
        public void SortByTwoFieldASC()
        {
            List<ExecutorsModel> executors = GetExecutors();
            var sortqueue = new Queue<AbstractSortField>();
            sortqueue.Enqueue(new AbstractSortField("DateExpiration", false));
            sortqueue.Enqueue(new AbstractSortField("DateCreate", true));
            executors = executors.Select(x => x.SortByMany(new Queue<AbstractSortField>(sortqueue))).ToList();
            Assert.Equal(new DateTime(2011, 3, 13), executors.ElementAt(0).TaskList.ElementAt(0).DateExpiration);
            Assert.Equal(new DateTime(2011, 3, 13), executors.ElementAt(1).TaskList.ElementAt(0).DateExpiration);
            Assert.Equal(new DateTime(2011, 3, 19), executors.ElementAt(0).TaskList.ElementAt(1).DateCreate);
            Assert.Equal(new DateTime(2011, 3, 19), executors.ElementAt(1).TaskList.ElementAt(1).DateCreate);
        }

        [Fact]
        public void SortByTwoFieldDESC()
        {
            List<ExecutorsModel> executors = GetExecutors();
            var sortqueue = new Queue<AbstractSortField>();
            sortqueue.Enqueue(new AbstractSortField("DateExpiration", false));
            sortqueue.Enqueue(new AbstractSortField("DateCreate", false));
            executors = executors.Select(x => x.SortByMany(new Queue<AbstractSortField>(sortqueue))).ToList();
            Assert.Equal(new DateTime(2011, 3, 13), executors.ElementAt(0).TaskList.ElementAt(0).DateExpiration);
            Assert.Equal(new DateTime(2011, 3, 13), executors.ElementAt(1).TaskList.ElementAt(0).DateExpiration);
            Assert.Equal(new DateTime(2011, 3, 20), executors.ElementAt(0).TaskList.ElementAt(1).DateCreate);
            Assert.Equal(new DateTime(2011, 3, 20), executors.ElementAt(1).TaskList.ElementAt(1).DateCreate);
        }

        private List<ExecutorsModel> GetExecutors()
        {
            var tasks1 = new List<AbstractTaskList>();
            tasks1.Add(new TaskListInDepartmentModel("", "Задача1", "Карпов", new DateTime(2011, 3, 21),
                                                     new DateTime(2011, 3, 13), DateTime.Now,
                                                     "4", "87", new ImageSet(), ""));
            tasks1.Add(new TaskListInDepartmentModel("", "Задача2", "Морозов", new DateTime(2011, 3, 20), null,
                                                     DateTime.Now,
                                                     "3", "87", new ImageSet(), ""));
            tasks1.Add(new TaskListInDepartmentModel("", "Задача3", "Петренко", new DateTime(2011, 3, 19), null,
                                                     DateTime.Now,
                                                     "2", "87", new ImageSet(), ""));
            var tasks2 = new List<AbstractTaskList>();
            tasks2.Add(new TaskListInDepartmentModel("", "Задача1", "Карпов", new DateTime(2011, 3, 21),
                                                     new DateTime(2011, 3, 13), DateTime.Now,
                                                     "4", "87", new ImageSet(), ""));
            tasks2.Add(new TaskListInDepartmentModel("", "Задача2", "Морозов", new DateTime(2011, 3, 20), null,
                                                     DateTime.Now,
                                                     "3", "87", new ImageSet(), ""));
            tasks2.Add(new TaskListInDepartmentModel("", "Задача3", "Петренко", new DateTime(2011, 3, 19), null,
                                                     DateTime.Now,
                                                     "2", "87", new ImageSet(), ""));
            var executor1 = new ExecutorsModel("Карпов", 8, tasks1);
            var executor2 = new ExecutorsModel("Морозов", 5, tasks2);
            var executors = new List<ExecutorsModel>();
            executors.Add(executor1);
            executors.Add(executor2);
            return executors;
        }
    }
}